package org.aceor.mddal.gateway.merger.function;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.aceor.mddal.gateway.merger.common.ResultSetQueryIndex;

import java.util.Map;

/**
 * Created by lxue on 16/4/17.
 */
public class AggregateValue {
    private Map<Integer, Object> rowSetIndexMap = Maps.newLinkedHashMap();
    private Map<String/** upperCase **/, Object> rowSetColumnNameMap = Maps.newLinkedHashMap();

    public Object getValue(ResultSetQueryIndex resultSetQueryIndex) {

        if (resultSetQueryIndex.isQueryByIndex()) {
            return rowSetIndexMap.get(resultSetQueryIndex.getQueryIndex());
        } else {
            return rowSetColumnNameMap.get(((String) resultSetQueryIndex.getQueryName().toUpperCase()));
        }

    }

    public void put(int index, String columnName, Object object) {
        Preconditions.checkArgument(index > 0);
        Preconditions.checkArgument(!Strings.isNullOrEmpty(columnName));
        Preconditions.checkNotNull(object);
        rowSetColumnNameMap.put(columnName.toUpperCase(), object);
        rowSetIndexMap.put(index, object);
    }
}
